package com.hxzy.service.impl;

import com.hxzy.bean.Discuss;
import com.hxzy.dao.DiscussDao;
import com.hxzy.dao.impl.DiscussDaoImpl;
import com.hxzy.service.DiscussService;
import com.hxzy.util.CommonUtil;
import com.hxzy.util.PageUtil;
import com.hxzy.vo.DiscussVo;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author nick
 */
public class DiscussServiceImpl implements DiscussService {

    private DiscussDao dao = new DiscussDaoImpl();
    @Override
    public int save(Discuss discuss) {
        return dao.save(discuss);
    }

    @Override
    public int update(Discuss discuss) {
        return dao.update(discuss);
    }

    @Override
    public int remove(Integer integer) {
        return dao.remove(integer);
    }

    @Override
    public List<Discuss> queryAll() {
        return dao.queryAll();
    }

    @Override
    public Discuss findById(Integer integer) {
        return dao.findById(integer);
    }

    @Override
    public PageUtil<DiscussVo> paging(int pageIndex,int pageSize, int messageId) {
        List<Discuss> paging = dao.paging(pageIndex, pageSize, messageId);
        List<DiscussVo> collect = paging.stream().map(t -> {
            //将Discuss对象转换为DiscussVo的对象
            DiscussVo vo = new DiscussVo();
            vo.setId(t.getId());
            vo.setContent(t.getContent());
            vo.setDateDialect(CommonUtil.date2dialect(t.getDate()));
            vo.setMessageId(t.getMessage().getId());
            vo.setNickName(t.getUser().getNickName());
            vo.setPic(t.getUser().getPic());
            return vo;
        }).collect(Collectors.toList());

        //查询有多少条数据
        int total = this.totals(messageId);

        PageUtil pageUtil = new PageUtil<>(pageIndex, pageSize, total, collect);
        return pageUtil;
    }

    @Override
    public int totals(int messageId) {
        return dao.totals(messageId);
    }

    @Override
    public int getMessageReplyCount(int messageId) {
        return 0;
    }
}
